/***************************************************************************
 创建者: hualei
 开始时间: 2018-12-20
 copyright: (C) 华友高科
 修改说明: (每次有修改就添加一条，带有 修改人，修改时间，修改描述)
 example (1) hualei 2018-12-20 类的具体实现代码编写

 ***************************************************************************
 *  ROBOT_MOVE 机械手控制指令具体实现                                                                       *
 ***************************************************************************/
#include "programset.h"
#include "regdata.h"
#include "programengine.h"
#include "robotmoveinstructionlogic.h"
#include "tcpstartinstruction.h"
#include "tcpmanager.h"
#include "GeneralDefine.h"
#include "messagelog.h"

extern TcpManager *tcpManager;

RobotMoveInstructionLogic::RobotMoveInstructionLogic(int robotIdIn, ProgramSet *programSetIn, ProgramEngine *programEngineIn, RegData *regDataIn)
{
    robotId=robotIdIn;
    programSet=programSetIn;
    programEngine=programEngineIn;
    regData = regDataIn;
}

E_PROGRAM_LOOP_RESULT RobotMoveInstructionLogic::startRobotMove(int programNumIn, const RobotMoveInstruction &instructionIn, int lineIndex)
{
    Q_UNUSED(lineIndex);
    if(instructionIn.instructionType != ROBOT_MOVE)
    {
        return E_PROGRAM_LOOP_RESULT_ERROR;
    }

    // 发送指令参数
    int result;
    if(instructionIn.robotMoveBusType == EM_ROBOT_MOVE_BUS_SERVER)
    {
       result = tcpManager->tcpWrite_server(instructionIn.localIP,instructionIn.communicationPort, instructionIn.sendParameterStr.toLatin1());
    }
    else if(instructionIn.robotMoveBusType == EM_ROBOT_MOVE_BUS_CLIENT)
    {
         result = tcpManager->tcpWrite_client(instructionIn.serverIP,instructionIn.communicationPort, instructionIn.sendParameterStr.toLatin1());
    }

    if(result != OK)
    {
        // 发送失败
        addMsg( ENUM_MSG_ERROR, "ProgramEngine", "RobotMoveInstructionLogic", 12070, robotId);

        return programEngine->startJump(programNumIn,instructionIn.jmpLableValue);
//        return E_PROGRAM_LOOP_RESULT_ERROR;
    }
    else
    {
        // 发送成功
        // 等待接收,超时处理
        QTime time;
        time.start();
        int elapsedTime = time.elapsed();
        // 限制超时等待
        QByteArray tmpReciveData;
        while(elapsedTime < instructionIn.timeOutValue * 1000)
        {
            // 未超时
            if(instructionIn.robotMoveBusType == EM_ROBOT_MOVE_BUS_CLIENT)
            {
                tcpManager->tcpRead_client(instructionIn.serverIP,instructionIn.communicationPort, tmpReciveData);
            }
            else if(instructionIn.robotMoveBusType == EM_ROBOT_MOVE_BUS_SERVER)
            {
                tcpManager->tcpRead_server(instructionIn.localIP,instructionIn.communicationPort, tmpReciveData);
            }

            if(tmpReciveData == instructionIn.waitParameterStr.toLatin1())
            {
                // 接收成功
                qDebug() << "RobotMove Recive Data:" << tmpReciveData;
                programEngine->calculateNextLineToRun(programNumIn);
                return E_PROGRAM_LOOP_RESULT_NORMAL;
            }
            usleep(1000);
            elapsedTime = time.elapsed();
        }
        // 超时－跳转标签
        return programEngine->startJump(programNumIn,instructionIn.jmpLableValue);
    }
}

void RobotMoveInstructionLogic::addMsg(int messageLevel, string componentName, string messageType, int messageCode, int robotId, int parameter1, int parameter2, int parameter3, int parameter4, QString message)
{
    Q_UNUSED(parameter2);
    Q_UNUSED(parameter3);
    Q_UNUSED(parameter4);

    Message tmpMsg;
    tmpMsg.MessageLevel = messageLevel;
    tmpMsg.componentClassName = componentName;
    tmpMsg.messageType = messageType;
    tmpMsg.robotId = robotId;
    tmpMsg.messageCode = messageCode;
   // tmpMsg.time = t1.tv_sec;

    QString infomationStr;

    switch(messageCode)
    {
       case 12070:
       {
            infomationStr = QObject::tr("机器人:") +
                QString::number(robotId) +
                QObject::tr("第") +
                QString::number(parameter1+1) +
                QObject::tr("行运行出现异常:机械手命令发送失败!")+message;

            break;
       }
     default:
    {

    }
    }
    tmpMsg.MessageInformation = infomationStr.toStdString();

    // qDebug()<<"53444444444444444444444"<< tmpMsg.messageCode;
    MessageLog::getInstance()->addMessage(tmpMsg);
}
